{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "true"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "### pry-docの読み込み\n",
    "#require \"/root/git_jupyter_notebook/Ruby/vendor/bundle/ruby/2.3.0/gems/pry-doc-0.10.0/lib/pry-doc\"\n",
    "require \"/Users/ftakao2007/jupyter/vendor/bundle/ruby/2.3.0/gems/pry-doc-0.10.0/lib/pry-doc\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 組み込み定数\n",
    "\n",
    "## 主な組み込み定数\n",
    "\n",
    "|組み込み定数|意味|\n",
    "|---|---|\n",
    "|STDIN|標準入力|\n",
    "|STDOUT|標準出力|\n",
    "|STDERR|標準エラー出力|\n",
    "|ENV|環境変数|\n",
    "|ARGF|仮想ファイル|\n",
    "|ARGV|Rubyスクリプトに与えられた引数|\n",
    "|DATA|`__END__` 以降をアクセスするFileオブジェクト|\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 150,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[\"kernel\", \"/root/.local/share/jupyter/runtime/kernel-c4ee4e16-b07e-47f3-bc26-8305294647ec.json\"]\n",
      "\n",
      "/root/jupyter/vendor/bundle/ruby/2.3.0/bin\n",
      "/usr/local/pyenv/versions/jupyter/bin\n",
      "/usr/local/pyenv/libexec\n",
      "/usr/local/pyenv/plugins/python-build/bin\n",
      "/usr/local/pyenv/plugins/pyenv-virtualenv/bin\n",
      "/usr/local/rbenv/versions/2.3.3/bin\n",
      "/usr/local/rbenv/libexec\n",
      "/usr/local/rbenv/plugins/ruby-build/bin\n",
      "/usr/local/rbenv/shims\n",
      "/usr/local/rbenv/bin\n",
      "/usr/local/pyenv/shims\n",
      "/usr/local/pyenv/bin\n",
      "/usr/local/pyenv/plugins/pyenv-virtualenv/shims\n",
      "/usr/local/sbin\n",
      "/usr/local/bin\n",
      "/usr/sbin\n",
      "/usr/bin\n",
      "/sbin\n",
      "/bin\n",
      "\n"
     ]
    }
   ],
   "source": [
    "### コマンドライン引数を出力\n",
    "puts ARGV\n",
    "puts\n",
    "\n",
    "### 環境変数を出力\n",
    "puts ENV[\"PATH\"].gsub(\":\", \"\\n\")\n",
    "puts"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 組み込み変数\n",
    "\n",
    "|組み込み定数|意味|\n",
    "|---|---|\n",
    "|$LOAD_PATH|Rubyをインストールしたディレクトリやカレントディレクトリが入っている|\n",
    "|-|コマンドラインで実行している場合はRubyを実行したカレントディレクトリも入る|\n",
    "|-|ここになければRUBYLIBを検索する|"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 159,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/root/jupyter/vendor/bundle/ruby/2.3.0/gems/iruby-0.3/lib\n",
      "/usr/local/rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/bundler-1.14.6/lib\n",
      "/usr/local/rbenv/rbenv.d/exec/gem-rehash\n",
      "/root/jupyter/vendor/bundle/ruby/2.3.0/gems/rbczmq-1.7.9/lib\n",
      "/root/jupyter/vendor/bundle/ruby/2.3.0/extensions/x86_64-linux/2.3.0-static/rbczmq-1.7.9\n",
      "/root/jupyter/vendor/bundle/ruby/2.3.0/gems/pry-doc-0.10.0/lib\n",
      "/root/jupyter/vendor/bundle/ruby/2.3.0/gems/yard-0.9.9/lib\n",
      "/root/jupyter/vendor/bundle/ruby/2.3.0/gems/pry-0.10.4/lib\n",
      "/root/jupyter/vendor/bundle/ruby/2.3.0/gems/slop-3.6.0/lib\n",
      "/root/jupyter/vendor/bundle/ruby/2.3.0/gems/method_source-0.8.2/lib\n",
      "/root/jupyter/vendor/bundle/ruby/2.3.0/gems/iruby-0.3/lib\n",
      "/root/jupyter/vendor/bundle/ruby/2.3.0/gems/multi_json-1.12.1/lib\n",
      "/root/jupyter/vendor/bundle/ruby/2.3.0/gems/mimemagic-0.3.2/lib\n",
      "/root/jupyter/vendor/bundle/ruby/2.3.0/gems/data_uri-0.1.0/lib\n",
      "/root/jupyter/vendor/bundle/ruby/2.3.0/gems/coderay-1.1.1/lib\n",
      "/usr/local/rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/bundler-1.14.6/lib/gems/bundler-1.14.6/lib\n",
      "/root/jupyter/vendor/bundle/ruby/2.3.0/gems/bond-0.5.1/lib\n",
      "/root/jupyter/vendor/bundle/ruby/2.3.0/extensions/x86_64-linux/2.3.0-static/bond-0.5.1\n",
      "/usr/local/rbenv/versions/2.3.3/lib/ruby/site_ruby/2.3.0\n",
      "/usr/local/rbenv/versions/2.3.3/lib/ruby/site_ruby/2.3.0/x86_64-linux\n",
      "/usr/local/rbenv/versions/2.3.3/lib/ruby/site_ruby\n",
      "/usr/local/rbenv/versions/2.3.3/lib/ruby/vendor_ruby/2.3.0\n",
      "/usr/local/rbenv/versions/2.3.3/lib/ruby/vendor_ruby/2.3.0/x86_64-linux\n",
      "/usr/local/rbenv/versions/2.3.3/lib/ruby/vendor_ruby\n",
      "/usr/local/rbenv/versions/2.3.3/lib/ruby/2.3.0\n",
      "/usr/local/rbenv/versions/2.3.3/lib/ruby/2.3.0/x86_64-linux\n",
      "\n",
      "nil\n",
      "\n",
      "nil\n",
      "\n",
      "nil\n"
     ]
    }
   ],
   "source": [
    "$LOAD_PATH.each{|x| puts x}\n",
    "puts\n",
    "\n",
    "p $RUBYLIB\n",
    "puts\n",
    "\n",
    "p $RUBYOPT\n",
    "puts\n",
    "\n",
    "p $PATH"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# Rubyインタプリタが利用する環境変数\n",
    "\n",
    "## 主な環境変数\n",
    "\n",
    "|環境変数|意味|\n",
    "|---|---|\n",
    "|`$RUBYOPT`|デフォルトで指定するコマンドラインオプション|\n",
    "|`$RUBYLIB`|デフォルトでライブラリを検索するPATH。これより先に `$LOAD_PATH` が読み込まれる。|\n",
    "|`$PATH`|Ruby上から実行したコマンドを検索するPATH|\n",
    "|`$RAILS_ENV`|Railsの環境(production, environment, testなど)を指定|\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# コマンドラインオプション\n",
    "\n",
    "## 主なコマンドラインオプション\n",
    "\n",
    "|オプション|意味|\n",
    "|---|---|\n",
    "|-h|ヘルプの表示|\n",
    "|-v|バージョンの表示|\n",
    "|-c|プログラムの文法チェック (実行はしない)|\n",
    "|-e|ワンライナー実行する|\n",
    "|-w|冗長モードで実行 (普段は出ないワーニングが出る)|\n",
    "|-Wn|(n = 0,1,2,(無し)) 出力範囲を指定した冗長モードでの実行。-W2もしくは-W は全て出力|\n",
    "|-I|(大文字のアイ) 組み込み変数 $LOAD_PATH に指定された文字列を追加する|\n",
    "|-r|スクリプト実行前に指定されたスクリプトを実行する|\n",
    "|-d|デバッグモードで実行する|\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# require,load など\n",
    "\n",
    "## 違い\n",
    "* require\n",
    "    * 何度実行しても1回しか実行されない。ファイルの拡張子を省略できる。ライブラリの読み込み用途\n",
    "* load\n",
    "    * 実行された回数だけ実行する。ファイルの拡張子は省略できない。設定情報の読み込み用途\n",
    "* include\n",
    "    * モジュールを読み込む\n",
    "\n",
    "## コマンドラインオプション「-I」の挙動について\n",
    "\n",
    "* 絶対PATHで指定\n",
    "    * そのPATHにファイルが存在するかどうかだけ見る。\n",
    "* 相対PATHで指定\n",
    "    * 組み込み変数 $LOAD_PATH に格納されたPATH順に探していく。\n",
    "* -Iで指定したPATHを最優先で検索する\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 特殊変数\n",
    "\n",
    "* 特殊変数とは\n",
    "  * 組み込み変数のうち、「`$`」と数字や記号で構成されたもの\n",
    "  * 例 (組み込み変数$LOAD_PATHを特殊変数で参照)\n",
    "     * `p $:`\n",
    "\n",
    "## 主な特殊変数\n",
    "\n",
    "|特殊変数|意味|\n",
    "|---|---|\n",
    "|`$_`|最後にgetsかreadlineで読み込んだ文字列|\n",
    "|`$.`|IO オブジェクトが最後に読み込んだ行の行番号|"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "gets\n",
      "\"123456789\\n\"\n",
      "\"abc\\n\"\n",
      "\n",
      "$_ = abc\n",
      "\n",
      "$. = 2\n"
     ]
    }
   ],
   "source": [
    "### $_\n",
    "Dir.mkdir(\"/tmp/tmp2\", 0755) unless Dir.exist?(\"/tmp/tmp2\")\n",
    "Dir.chdir(\"/tmp/tmp2\")\n",
    "File.open(\"io_read.txt\", \"w\") do |file|\n",
    "  file.write \"123456789\\nabc\\nxyz\\n\"\n",
    "end\n",
    "puts\n",
    "\n",
    "puts \"gets\"\n",
    "io = open(\"io_read.txt\")\n",
    "p io.gets\n",
    "p io.gets\n",
    "#p io.gets\n",
    "io.close\n",
    "puts\n",
    "\n",
    "puts \"$_ = #{$_}\"\n",
    "puts \"$. = #{$.}\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "|特殊変数|意味|\n",
    "|---|---|\n",
    "|`$&`|最後にマッチした文字列|\n",
    "|$`|マッチした部分より前の文字列|\n",
    "|`$'`|マッチした部分より後ろの文字列|\n",
    "|`$~`|最後に成功したマッチに対する結果。`$~[n]`で参照できる|"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "マッチした文字列を取り出す\n",
      "ove Ruby. I love Ruby.  I love Rub\n",
      "\n",
      "マッチした文字列の直前の文字列を取り出す\n",
      "I l\n",
      "\n",
      "マッチした文字列の後の文字列を取り出す\n",
      "y.\n",
      "\n",
      "最後に成功したマッチした時の情報(MatchDataオブジェクト)。グループ分けは無いので$~[0]のみ。\n",
      "#<MatchData \"ove Ruby. I love Ruby.  I love Rub\">\n",
      "\"ove Ruby. I love Ruby.  I love Rub\"\n",
      "\n"
     ]
    }
   ],
   "source": [
    "### マッチ関連\n",
    "\n",
    "#/ve Ru/ =~ \"I love Ruby. I love Ruby.  I love Ruby.\"\n",
    "#\"I love Ruby. I love Ruby. I love Ruby.\".scan(/ve Ru/)\n",
    "#/[ovr].*b/ =~ \"I love Ruby. I love Ruby.  I love Ruby.\"\n",
    "/o.*b/ =~ \"I love Ruby. I love Ruby.  I love Ruby.\"\n",
    "\n",
    "puts \"マッチした文字列を取り出す\"\n",
    "puts $&\n",
    "puts\n",
    "\n",
    "puts \"マッチした文字列の直前の文字列を取り出す\"\n",
    "puts $`\n",
    "puts\n",
    "\n",
    "puts \"マッチした文字列の後の文字列を取り出す\"\n",
    "puts $'\n",
    "puts\n",
    "\n",
    "puts '最後に成功したマッチした時の情報(MatchDataオブジェクト)。グループ分けは無いので$~[0]のみ。'\n",
    "p $~\n",
    "p $~[0]\n",
    "puts"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "|特殊変数|意味|\n",
    "|---|---|\n",
    "|`$0`|現在実行中のプログラムファイル名|\n",
    "|`$1, $2, ...`|n番目のカッコに対応する文字列|\n",
    "|`$~`|最後に成功したマッチに対する結果。`$~[n]`で参照できる|\n",
    "|`$+`|マッチした中で最後のカッコに対応する文字列|\n",
    "\n",
    "* `$0` は<font color=\"red\">実行しているプログラムのPATHになってしまうことに注意</font>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "$nで文字列を参照\n",
      "\"/root/jupyter/vendor/bundle/ruby/2.3.0/bin/iruby\"\n",
      "\"is \"\n",
      "\"1980\"\n",
      "\"yen\"\n",
      "nil\n",
      "\n",
      "$~はMatchDataオブジェクトが返る\n",
      "#<MatchData \"is 1980yen\" 1:\"is \" 2:\"1980\" 3:\"yen\">\n",
      "\n",
      "$~[n]で文字列を参照\n",
      "\"is 1980yen\"\n",
      "\"is \"\n",
      "\"1980\"\n",
      "\"yen\"\n",
      "nil\n",
      "\n",
      "最後にマッチした文字列を出力\n",
      "\"yen\"\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "\"yen\""
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "/(is\\s)(\\d+)(yen)/ =~ \"price is 1980yen\"\n",
    "\n",
    "puts '$nで文字列を参照'\n",
    "p $0\n",
    "p $1\n",
    "p $2\n",
    "p $3\n",
    "p $4\n",
    "puts\n",
    "\n",
    "puts '$~はMatchDataオブジェクトが返る'\n",
    "p $~\n",
    "puts\n",
    "\n",
    "puts '$~[n]で文字列を参照'\n",
    "p $~[0]\n",
    "p $~[1]\n",
    "p $~[2]\n",
    "p $~[3]\n",
    "p $~[4]\n",
    "puts\n",
    "\n",
    "puts \"最後にマッチした文字列を出力\"\n",
    "p $+"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "|特殊変数|意味|\n",
    "|---|---|\n",
    "|`$?`|最後に終了した子プロセスのステータス|\n",
    "|`$$`|プロセスID|\n",
    "|`$@`|バックトレースを表す配列|\n",
    "|`$!`|直近で捕捉した例外オブジェクト|\n",
    "\n",
    "`$?` はforkとかスレッドとか関連ぽいので保留"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "プロセスID\n",
      "11053\n",
      "\n",
      "バックトレースを出力\n",
      "[\"(pry):962:in `<main>'\", \"/root/jupyter/vendor/bundle/ruby/2.3.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:355:in `eval'\", \"/root/jupyter/vendor/bundle/ruby/2.3.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:355:in `evaluate_ruby'\", \"/root/jupyter/vendor/bundle/ruby/2.3.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:323:in `handle_line'\", \"/root/jupyter/vendor/bundle/ruby/2.3.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:243:in `block (2 levels) in eval'\", \"/root/jupyter/vendor/bundle/ruby/2.3.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:242:in `catch'\", \"/root/jupyter/vendor/bundle/ruby/2.3.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:242:in `block in eval'\", \"/root/jupyter/vendor/bundle/ruby/2.3.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:241:in `catch'\", \"/root/jupyter/vendor/bundle/ruby/2.3.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:241:in `eval'\", \"/root/jupyter/vendor/bundle/ruby/2.3.0/gems/iruby-0.3/lib/iruby/backend.rb:66:in `eval'\", \"/root/jupyter/vendor/bundle/ruby/2.3.0/gems/iruby-0.3/lib/iruby/backend.rb:12:in `eval'\", \"/root/jupyter/vendor/bundle/ruby/2.3.0/gems/iruby-0.3/lib/iruby/kernel.rb:87:in `execute_request'\", \"/root/jupyter/vendor/bundle/ruby/2.3.0/gems/iruby-0.3/lib/iruby/kernel.rb:47:in `dispatch'\", \"/root/jupyter/vendor/bundle/ruby/2.3.0/gems/iruby-0.3/lib/iruby/kernel.rb:37:in `run'\", \"/root/jupyter/vendor/bundle/ruby/2.3.0/gems/iruby-0.3/lib/iruby/command.rb:70:in `run_kernel'\", \"/root/jupyter/vendor/bundle/ruby/2.3.0/gems/iruby-0.3/lib/iruby/command.rb:34:in `run'\", \"/root/jupyter/vendor/bundle/ruby/2.3.0/gems/iruby-0.3/bin/iruby:5:in `<top (required)>'\", \"/root/jupyter/vendor/bundle/ruby/2.3.0/bin/iruby:22:in `load'\", \"/root/jupyter/vendor/bundle/ruby/2.3.0/bin/iruby:22:in `<main>'\"]\n",
      "\n",
      "例外オブジェクトを出力\n",
      "#<NameError: undefined local variable or method `hoge' for main:Object>\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "#<NameError: undefined local variable or method `hoge' for main:Object>"
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "puts \"プロセスID\"\n",
    "puts $$\n",
    "puts\n",
    "\n",
    "begin\n",
    "  a = hoge\n",
    "rescue\n",
    "  puts \"バックトレースを出力\"\n",
    "  p $@\n",
    "  puts\n",
    "  puts \"例外オブジェクトを出力\"\n",
    "  p $!\n",
    "end"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "|特殊変数|意味|\n",
    "|---|---|\n",
    "|`$*`|Rubyスクリプトに与えられた引数。 ARGVと同じ。|\n",
    "|`$:`|loadやrequireの検索PATH。 `$LOAD_PATH`と同じ。|"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Rubyスクリプトに与えられた引数\n",
      "[\"kernel\", \"/root/.local/share/jupyter/runtime/kernel-c4ee4e16-b07e-47f3-bc26-8305294647ec.json\"]\n",
      "\n",
      "loadやrequireの検索PATH\n",
      "[\"/root/jupyter/vendor/bundle/ruby/2.3.0/gems/iruby-0.3/lib\", \"/usr/local/rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/bundler-1.14.6/lib\", \"/usr/local/rbenv/rbenv.d/exec/gem-rehash\", \"/root/jupyter/vendor/bundle/ruby/2.3.0/gems/rbczmq-1.7.9/lib\", \"/root/jupyter/vendor/bundle/ruby/2.3.0/extensions/x86_64-linux/2.3.0-static/rbczmq-1.7.9\", \"/root/jupyter/vendor/bundle/ruby/2.3.0/gems/pry-doc-0.10.0/lib\", \"/root/jupyter/vendor/bundle/ruby/2.3.0/gems/yard-0.9.9/lib\", \"/root/jupyter/vendor/bundle/ruby/2.3.0/gems/pry-0.10.4/lib\", \"/root/jupyter/vendor/bundle/ruby/2.3.0/gems/slop-3.6.0/lib\", \"/root/jupyter/vendor/bundle/ruby/2.3.0/gems/method_source-0.8.2/lib\", \"/root/jupyter/vendor/bundle/ruby/2.3.0/gems/iruby-0.3/lib\", \"/root/jupyter/vendor/bundle/ruby/2.3.0/gems/multi_json-1.12.1/lib\", \"/root/jupyter/vendor/bundle/ruby/2.3.0/gems/mimemagic-0.3.2/lib\", \"/root/jupyter/vendor/bundle/ruby/2.3.0/gems/data_uri-0.1.0/lib\", \"/root/jupyter/vendor/bundle/ruby/2.3.0/gems/coderay-1.1.1/lib\", \"/usr/local/rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/bundler-1.14.6/lib/gems/bundler-1.14.6/lib\", \"/root/jupyter/vendor/bundle/ruby/2.3.0/gems/bond-0.5.1/lib\", \"/root/jupyter/vendor/bundle/ruby/2.3.0/extensions/x86_64-linux/2.3.0-static/bond-0.5.1\", \"/usr/local/rbenv/versions/2.3.3/lib/ruby/site_ruby/2.3.0\", \"/usr/local/rbenv/versions/2.3.3/lib/ruby/site_ruby/2.3.0/x86_64-linux\", \"/usr/local/rbenv/versions/2.3.3/lib/ruby/site_ruby\", \"/usr/local/rbenv/versions/2.3.3/lib/ruby/vendor_ruby/2.3.0\", \"/usr/local/rbenv/versions/2.3.3/lib/ruby/vendor_ruby/2.3.0/x86_64-linux\", \"/usr/local/rbenv/versions/2.3.3/lib/ruby/vendor_ruby\", \"/usr/local/rbenv/versions/2.3.3/lib/ruby/2.3.0\", \"/usr/local/rbenv/versions/2.3.3/lib/ruby/2.3.0/x86_64-linux\"]\n"
     ]
    }
   ],
   "source": [
    "puts \"Rubyスクリプトに与えられた引数\"\n",
    "puts $*\n",
    "puts\n",
    "\n",
    "puts \"loadやrequireの検索PATH\"\n",
    "puts $:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "|特殊変数|意味|\n",
    "|---|---|\n",
    "|`$\\`|出力レコードセパレータを表す文字(print が最後にこの文字列を出力する)デフォルトはnil|\n",
    "|`$,`|出力フィールドの区切り文字。Array#joinの引数が無い時やprintの各引数の間で出力される。デフォルトはnil|\n",
    "\n",
    "備考 : `$/` 入力レコードの区切り文字。デフォルトは\\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "出力レコードセパレータを表す文字。デフォルトは「nil」\n",
      "\"end\"\n",
      "1end\n",
      "\n",
      "出力フィールドの区切り文字。デフォルトは「nil」\n",
      "\"-\"\n",
      "1-2-3\n",
      "\n"
     ]
    }
   ],
   "source": [
    "puts '出力レコードセパレータを表す文字。デフォルトは「nil」'\n",
    "$\\ = \"end\"\n",
    "p $\\\n",
    "print \"1\"\n",
    "puts\n",
    "puts\n",
    "\n",
    "puts '出力フィールドの区切り文字。デフォルトは「nil」'\n",
    "$, = \"-\"\n",
    "p $,\n",
    "puts [1,2,3].join\n",
    "puts\n",
    "\n",
    "# puts '入力レコードの区切り文字。デフォルトは「\\n」'\n",
    "# p $/\n",
    "# puts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\u001b[1mFrom:\u001b[0m string.c (C Method):\n",
      "\u001b[1mOwner:\u001b[0m String\n",
      "\u001b[1mVisibility:\u001b[0m public\n",
      "\u001b[1mSignature:\u001b[0m scan(arg1)\n",
      "\u001b[1mNumber of lines:\u001b[0m 24\n",
      "\n",
      "Both forms iterate through \u001b[1mstr\u001b[0m, matching the pattern (which may be a\n",
      "\u001b[1;34;4mRegexp\u001b[0m or a \u001b[1;34;4mString\u001b[0m). For each match, a result is\n",
      "generated and either added to the result array or passed to the block. If\n",
      "the pattern contains no groups, each individual result consists of the\n",
      "matched string, \u001b[1;32m$&\u001b[0m.  If the pattern contains groups, each\n",
      "individual result is itself an array containing one entry per group.\n",
      "\n",
      "   a = \u001b[31m\u001b[1;31m\"\u001b[0m\u001b[31mcruel world\u001b[1;31m\"\u001b[0m\u001b[31m\u001b[0m\n",
      "   a.scan(\u001b[35m\u001b[1;35m/\u001b[0m\u001b[35m\u001b[1;35m\\w\u001b[0m\u001b[35m+\u001b[1;35m/\u001b[0m\u001b[35m\u001b[0m)        \u001b[1;34m#=> [\"cruel\", \"world\"]\u001b[0m\n",
      "   a.scan(\u001b[35m\u001b[1;35m/\u001b[0m\u001b[35m...\u001b[1;35m/\u001b[0m\u001b[35m\u001b[0m)        \u001b[1;34m#=> [\"cru\", \"el \", \"wor\"]\u001b[0m\n",
      "   a.scan(\u001b[35m\u001b[1;35m/\u001b[0m\u001b[35m(...)\u001b[1;35m/\u001b[0m\u001b[35m\u001b[0m)      \u001b[1;34m#=> [[\"cru\"], [\"el \"], [\"wor\"]]\u001b[0m\n",
      "   a.scan(\u001b[35m\u001b[1;35m/\u001b[0m\u001b[35m(..)(..)\u001b[1;35m/\u001b[0m\u001b[35m\u001b[0m)   \u001b[1;34m#=> [[\"cr\", \"ue\"], [\"l \", \"wo\"]]\u001b[0m\n",
      "\n",
      "And the block form:\n",
      "\n",
      "   a.scan(\u001b[35m\u001b[1;35m/\u001b[0m\u001b[35m\u001b[1;35m\\w\u001b[0m\u001b[35m+\u001b[1;35m/\u001b[0m\u001b[35m\u001b[0m) {|w| print \u001b[31m\u001b[1;31m\"\u001b[0m\u001b[31m<<#{w}\u001b[0m\u001b[31m>> \u001b[1;31m\"\u001b[0m\u001b[31m\u001b[0m }\n",
      "   print \u001b[31m\u001b[1;31m\"\u001b[0m\u001b[31m\u001b[1;35m\\n\u001b[0m\u001b[31m\u001b[1;31m\"\u001b[0m\u001b[31m\u001b[0m\n",
      "   a.scan(\u001b[35m\u001b[1;35m/\u001b[0m\u001b[35m(.)(.)\u001b[1;35m/\u001b[0m\u001b[35m\u001b[0m) {|x,y| print y, x }\n",
      "   print \u001b[31m\u001b[1;31m\"\u001b[0m\u001b[31m\u001b[1;35m\\n\u001b[0m\u001b[31m\u001b[1;31m\"\u001b[0m\u001b[31m\u001b[0m\n",
      "\n",
      "\u001b[1mproduces:\u001b[0m\n",
      "\n",
      "   \u001b[31m\u001b[1;31m<<cruel\u001b[0m\u001b[31m\u001b[0m>> \u001b[31m\u001b[1;31m<<world\u001b[0m\u001b[31m\u001b[0m>>\u001b[31m\n",
      "   rceu lowlr\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "show-doc String#scan"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# リファレンス表示ツールri\n",
    "\n",
    "## riの使い方\n",
    "\n",
    "* クラス\n",
    "    * ri クラス名\n",
    "* クラスメソッド\n",
    "    * ri クラス名.メソッド名\n",
    "* インスタンスメソッド\n",
    "    * ri クラス名#メソッド名"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# RDoc書式\n",
    "\n",
    "## マジックコメント\n",
    "\n",
    "` /coding[:=]\\s*[\\w.-]+/ ` という正規表現にマッチする形式で記述する\n",
    "\n",
    "## 埋め込みドキュメント\n",
    "\n",
    "```\n",
    "=begin\n",
    " ここに任意のドキュメントを記載できる\n",
    "=end\n",
    "```"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Ruby 2.3.1",
   "language": "ruby",
   "name": "ruby"
  },
  "language_info": {
   "file_extension": ".rb",
   "mimetype": "application/x-ruby",
   "name": "ruby",
   "version": "2.3.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
